# -*- coding: utf-8 -*-

import warnings
from itertools import accumulate

from sympy import false

warnings.filterwarnings('ignore')
from ultralytics import YOLO

if __name__ == '__main__':

    # model = YOLO(model=r'D:\Edge\ultralytics-main\ultralytics\cfg\models\11\yolo11.yaml')
    model = YOLO(model=r'D:\Clion\code\20241124\yolo11.yaml')
    model.load(r'D:\Clion\code\20241124\yolo11n.pt')

    # # 解冻所有层
    # for param in model.parameters():
    #     param.requires_grad = True
    #
    # model.train(
    #     data=r'D:\Clion\code\20241124\data.yaml',
    #     imgsz=256,
    #     epochs=50,
    #     batch=8,
    #     workers=0,
    #     device='0',
    #     optimizer='AdamW',  # 改用AdamW优化器 🌟
    #     lr0=2e-4,  # 初始学习率
    #     lrf=0.005,  # 最终学习率0.0005
    #     cos_lr=True,  # 启用cosine退火
    #     warmup_epochs=20,  # 延长热启动
    #     weight_decay=0.08,  # 调整权重衰减
    #     momentum=0.9,
    #     box=8.0,  # 调整损失权重
    #     cls=2.0,  # 🌟
    #     dfl=1.8,  #
    #     # freeze=[0,1,2,3,4],  # 仅冻结stem层 🌟
    #     label_smoothing=0.1,
    #     patience=30,  # 延长早停等待
    #     overlap_mask=True,
    #     mask_ratio=4,
    #     augment=True,  # 强制启用增强
    #     erasing=0.3,  # 随机擦除概率
    #     close_mosaic=15,  # 延迟关闭mosaic
    #     nbs=64,
    #     amp=False,
    # )
    # model.load("D:\Clion\\code\\20241124\\runs\\train\\exp3\\weights\\last.pt")
    # 修改后的训练配置
    model.train(
        data=r'D:\Clion\code\20241124\data_1.yaml',
        imgsz=256,
        epochs=120,
        batch=8,
        workers=0,
        device='0',
        optimizer='AdamW',  # 改用AdamW优化器 🌟
        lr0=2e-4,  # 初始学习率
        lrf=0.005,  # 最终学习率0.0005
        cos_lr=True,  # 启用cosine退火
        warmup_epochs=20,  # 延长热启动
        weight_decay=0.08,  # 调整权重衰减
        momentum=0.9,
        box=8.0,  # 调整损失权重
        cls=2.0,  # 🌟
        dfl=1.8,  #
        # freeze=[],  # 仅冻结stem层 🌟
        label_smoothing=0.1,
        patience=30,  # 延长早停等待
        overlap_mask=True,
        mask_ratio=4,
        augment=True,  # 强制启用增强
        erasing=0.3,  # 随机擦除概率
        close_mosaic=15,  # 延迟关闭mosaic
        nbs=64,
        resume=True,
        amp=False,
    )


# 3 5 10 27 32 34